package handler

import (
	"github.com/gin-gonic/gin"
	"short-video-hub/model"
	"short-video-hub/pkg"
)

func Register(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	if username == "" || password == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不可为空",
		})
		return
	}
	data := model.User{
		Username: username,
		Password: pkg.Md5(password),
	}
	model.DB.Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "OK",
	})
	return
}
func Login(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	var user model.User
	err := model.DB.Where("username=?", username).Find(&user).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "不存在的不允许登录",
		})
		return
	}
	if user.Password != pkg.Md5(password) {
		if err != nil {
			c.JSON(200, gin.H{
				"code": 500,
				"msg":  "密码错误",
			})
			return
		}
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "登录成功",
		"data": user,
	})
	return
}
